﻿using Accountant.DataModel;
using NUnit.Framework;
using FluentAssertions;

namespace UnitTests
{
  [TestFixture]
  public class BoundIntegrity
  {
    [Test]
    public void AddDerivedAccountChangesBaseAccount()
    {
      var ctx = new AccountantContext();
      var bse = ctx.Accounts.Add(new Account { Name = "Base"});
      var derived = ctx.Accounts.Add(new Account { Name = "Derived"});
      bse.DerivedAccounts.Add(derived);
      derived.BaseAccount.Should().BeSameAs(bse);
    }
    [Test]
    public void SetBaseAccountShouldAddDerivedAccount()
    {
      var ctx = new AccountantContext();
      var bse = ctx.Accounts.Add(new Account { Name = "Base"});
      var derived = ctx.Accounts.Add(new Account { Name = "Derived"});
      derived.BaseAccount = bse;
      bse.DerivedAccounts.Should().Equal(derived);
    }
    [Test]
    public void OneToManyInegrityIsLazy()
    {
      var ctx = new AccountantContext();
      var bse = ctx.Accounts.Add(new Account { Name = "Base"});
      var derived = ctx.Accounts.Add(new Account { Name = "Derived" });
      derived.BaseAccount = bse;
      bse._DerivedAccounts.Should().BeNull();
    }
  }
}